﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using JDEduTrain.Framework.Entity;
using JDEduTrain.Framework.Business;
using Wicresoft.Framework.Organization;
using NameControlLib;

public partial class Page_FinishDetail : BaseAuthenticationPage
{
    public Guid ClassID
    {
        get
        {
            if (Request["ClassID"] != null)
                return new Guid(Request["ClassID"]);
            return Guid.Empty;
        }
    }

    public string ClassMemeberIDs
    {
        get
        {
            if (ViewState["ClassMemeberIDs"] != null)
                return ViewState["ClassMemeberIDs"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["ClassMemeberIDs"] = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        this.UseTabber = true;
        if (!IsPostBack)
        {
            TrainClass trainClass = new TrainClass(ClassID);
            if (trainClass.Load())
            {

                //班级状态为未结束时，不允许编辑学员学分
                if (trainClass.FinishStatus != "已结束")
                {
                    trToolMenu.Visible = false;
                }
                else
                {
                    trToolMenu.Visible = true;
                }
            }
            BindClass();
        }

    }
    protected DataTable DTUserPageList
    {
        get
        {
            if (ViewState["DTUserPageList"] != null)
            {
                return (DataTable)ViewState["DTUserPageList"];
            }
            return new DataTable();
        }
        set
        {
            ViewState["DTUserPageList"] = value;
        }
    }




    protected void BindClass()
    {
        int recordCount = 0;
        TrainClass trainClass = new TrainClass(ClassID);
        if (trainClass.Load())
        {
            tbBeginTime.Text = trainClass.BeginTime.ToShortDateString();
            tbClassName.Text = trainClass.ClassName;
            tbComment.Text = trainClass.Comment;
            tbEndTime.Text = trainClass.EndTime.ToShortDateString();
            tbCourseInfo.Text = trainClass.CourseInfo;
            tbScore.Text = trainClass.Score.ToString();
            tbPlace.Text = trainClass.Place;
            tbLectorInfo.Text = trainClass.LectorInfo;
            tbCurriculum.Text = trainClass.Curriculum;
            tbTrainLevel.Text = trainClass.TrainLevel;
            tbTrainDay.Text = trainClass.TrainDayCount.ToString();
            tbHostUnit.Text = trainClass.HostUnit;
            tbDeptDisplayName.Text = trainClass.ApplyOrgan;
            lClassScore.Text = trainClass.Score.ToString();
            cvScore.ValueToCompare = trainClass.Score.ToString();
        }
        string memberStatus = "终审通过";
        DTUserPageList = BLTrain.GetMemberByClassID(ClassID, memberStatus, dpPager.CurrentPageIndex, dpPager.PageSize, "1=1", ClassMember.SQLCOL_MEMBERNAME + " ASC", ref recordCount);
        DTUserPageList.Columns.Add("OrderName");
        foreach (DataRow dr in DTUserPageList.Rows)
        {
            try
            {
                ClassOrder order = new ClassOrder(new Guid(dr["OrderID"].ToString()), new Guid(dr["ClassID"].ToString()));
                if (order.Load())
                {
                    dr["OrderName"] = order.OrderName;
                }
            }
            catch (Exception)
            {
            }
        }
        rptMemberInfo.DataSource = DTUserPageList;
        rptMemberInfo.DataBind();

        dpPager.RecordCount = recordCount;

        DataTable dtOrder = BLTrain.GetOrderByClassID(ClassID);
        rptOrderInfo.DataSource = dtOrder;
        rptOrderInfo.DataBind();



    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindClass();
    }

    protected void btnEditScore_Click(object sender, EventArgs e)
    {
        string[] memberIDs = ClassMemeberIDs.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string memberID in memberIDs)
        {
            ClassMember member = new ClassMember(new Guid(memberID), ClassID);
            if (member.Load())
            {
                try
                {
                    member.TotalScore = double.Parse(tbEditScore.Text);
                }
                catch (Exception)
                {
                }
                member.Save();
            }
            MemberTrainDetail memberTrainDetail = new MemberTrainDetail();
            if (memberTrainDetail.Load(string.Format("{0} = '{1}' AND {2} = '{3}'", MemberTrainDetail.SQLCOL_MEMBERID, new Guid(memberID), MemberTrainDetail.SQLCOL_CLASSNAME, tbClassName.Text)))
            {
                memberTrainDetail.Score = member.TotalScore;
                memberTrainDetail.Save();
            }

        }
        tbEditScore.Text = string.Empty;
        BindClass();

    }

    protected void btnEditMember_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rptMemberInfo.Items.Count; i++)
        {
            if ((rptMemberInfo.Items[i].FindControl("cbUser") as CheckBox).Checked)
            {
                Guid id = new Guid(DTUserPageList.Rows[rptMemberInfo.Items[i].ItemIndex]["ID"].ToString());
                sb.Append(id);
                sb.Append(";");
            }
        }
        ClassMemeberIDs = sb.ToString();
        if (sb.Length > 0)
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "edit", "openEditScore();", true);
        }
        else
        {
            ClassMemeberIDs = string.Empty;
            Page.ClientScript.RegisterStartupScript(GetType(), "alert", "alert('请选择学员');", true);
        }

    }

    protected void rptMemberInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            HtmlTableCell tc = e.Item.FindControl("tdCb") as HtmlTableCell;
            //if (OrganizationHelper.IsUserInRole(CurrentUser.ID, AppConfig.OrganizeAdminRoleID))
            //{
            //    tc.Visible = false;
            //}
            Label lFullName = e.Item.FindControl("lFullName") as Label;

            string id = DTUserPageList.Rows[e.Item.ItemIndex]["ID"].ToString();
            NameControlLib.NameInfo userControl = new NameInfo();
            userControl.ID = "UserNameControl_" + id;
            userControl.UserID = new Guid(id);
            userControl.ScriptPath = "../Js/NameInfo.js";
            lFullName.Controls.Add(userControl);
        }
    }
    protected void dpPager_PreRender(object sender, EventArgs e)
    {
        BindClass();
    }



}
